#include <stdint.h>
#include "stm32f1xx_hal.h"
#include "stm32f1xx_hal_def.h"
#include "stm32f1xx_hal_gpio.h"
#include "FreeRTOS.h"
#include "task.h"
#include "projdefs.h"
#include "event_groups.h"
#include "timers.h"

#define START_TASK_PRIO 1 //任务优先级
#define START_STK_SIZE 128 //任务堆栈大小
TaskHandle_t StartTask_Handler; //任务句柄
static void start_task(void *pvParameters); //任务函数
#define LED0_TASK_PRIO 2 //任务优先级
#define LED0_STK_SIZE 50 //任务堆栈大小
TaskHandle_t LED0Task_Handler; //任务句柄
// void led0_task(void *p_arg); //任务函数
#define LED1_TASK_PRIO 3 //任务优先级
#define LED1_STK_SIZE 50 //任务堆栈大小
TaskHandle_t LED1Task_Handler; //任务句柄
// void led1_task(void *p_arg); //任务函数


static void led0_task(void *pvParameters)
{
    static uint8_t st = 0;
    while(1) {
        // LED0=~LED0;
        /*Configure GPIO pin Output Level */
        st = ~st;
        HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, st);
        vTaskDelay(500);
    }
}
//LED1 任务函数
static void led1_task(void *pvParameters)
{
    while(1) {
        // LED1=0;
        /*Configure GPIO pin Output Level */
        HAL_GPIO_WritePin(GPIOE, GPIO_PIN_5, GPIO_PIN_RESET);
        vTaskDelay(200);
        // LED1=1;
        /*Configure GPIO pin Output Level */
        HAL_GPIO_WritePin(GPIOE, GPIO_PIN_5, GPIO_PIN_SET);
        vTaskDelay(800);
    }
}
static EventGroupHandle_t event_group_handle;
TimerHandle_t timer_handle;

#define TIMER_10MS_EVENT_BIT (1 << 0)

static void timer_10ms_callback(TimerHandle_t xTimer)
{
    // EventBits_t xEventGroupSetBits( EventGroupHandle_t xEventGroup,
    //                             const EventBits_t uxBitsToSet ) PRIVILEGED_FUNCTION;
    EventBits_t event_bits = xEventGroupSetBits(event_group_handle, TIMER_10MS_EVENT_BIT);
} 

// TimerHandle_t xTimerCreate( const char * const pcTimerName,
//                                 const TickType_t xTimerPeriodInTicks,
//                                 const BaseType_t xAutoReload,
//                                 void * const pvTimerID,
//                                 TimerCallbackFunction_t pxCallbackFunction ) PRIVILEGED_FUNCTION;
static void task_10ms(void *pvParameters)
{
    EventBits_t event_bits_wait;
    event_group_handle = xEventGroupCreate();
    TimerHandle_t timer_handle = xTimerCreate((const char*	)"timer 10ms",(TickType_t 	)10,(UBaseType_t	)pdTRUE,(void *		)1,(TimerCallbackFunction_t)timer_10ms_callback);
    xTimerStart(timer_handle, 0);

    for (;;) {
        event_bits_wait = xEventGroupWaitBits(event_group_handle, TIMER_10MS_EVENT_BIT, pdTRUE, pdFALSE, portMAX_DELAY);

        static uint16_t tick_timer = 0;

        tick_timer++;
        if (tick_timer == 100) {
            HAL_GPIO_WritePin(GPIOE, GPIO_PIN_5, GPIO_PIN_RESET);
        } else if (200 == tick_timer) {
            tick_timer = 0;
            HAL_GPIO_WritePin(GPIOE, GPIO_PIN_5, GPIO_PIN_SET);
        }

    }
}
#if 0
static void start_task(void *pvParameters)
{
    taskENTER_CRITICAL(); //进入临界区
    // //创建 LED0 任务
    // xTaskCreate((TaskFunction_t )led0_task,
    //             (const char* )"led0_task",
    //             (uint16_t )LED0_STK_SIZE,
    //             (void* )NULL,
    //             (UBaseType_t )LED0_TASK_PRIO,
    //             (TaskHandle_t* )&LED0Task_Handler);
    // //创建 LED1 任务
    // xTaskCreate((TaskFunction_t )led1_task,
    //             (const char* )"led1_task",
    //             (uint16_t )LED1_STK_SIZE,
    //             (void* )NULL,
    //             (UBaseType_t )LED1_TASK_PRIO,
    //             (TaskHandle_t* )&LED1Task_Handler);

        //创建 LED1 任务
    xTaskCreate((TaskFunction_t )task_10ms,
                (const char* )"led1_task",
                (uint16_t )LED1_STK_SIZE,
                (void* )NULL,
                (UBaseType_t )LED1_TASK_PRIO,
                (TaskHandle_t* )&LED1Task_Handler);
    vTaskDelete(StartTask_Handler); //删除开始任务
    taskEXIT_CRITICAL(); //退出临界区
}
#endif

void create_start_task(void)
{
    //     //创建开始任务
    // xTaskCreate((TaskFunction_t )start_task, //任务函数
    //     (const char* )"start_task", //任务名称
    //     (uint16_t )START_STK_SIZE, //任务堆栈大小
    //     (void* )NULL, //传递给任务函数的参数
    //     (UBaseType_t )START_TASK_PRIO, //任务优先级
    //     (TaskHandle_t* )&StartTask_Handler); //任务句柄
    // vTaskStartScheduler(); //开启任务调度
}

